package com.example.springmongo.service;

import com.example.springmongo.po.User;
import com.example.springmongo.repository.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.jws.soap.SOAPBinding;
import java.util.List;
import java.util.Optional;

/**
 * @author lzh
 * @date 2019/9/17 - 9:26
 */
@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private MongoTemplate template;


    /**
     * 通过name和description（模糊查询）来查询用户分页信息，
     * 并且查询结果使用age字段降序排序。
     * 方法返回Page对象
     *
     * @param size  size表示每页显示的条数
     * @param page  page表示当前页码数，0表示第一页
     * @param user
     * @return
     */
    public Page<User> getUserByCondition(int size , int page , User user){
        Query query = new Query();

        //Criteria条件
        Criteria criteria = new Criteria();
        if (!StringUtils.isEmpty(user.getName())){
            criteria.and("name").is(user.getName());
        }
        if (!StringUtils.isEmpty(user.getDescription())){
            criteria.and("description").is(user.getDescription());
        }
        query.addCriteria(criteria);

        Sort sort = new Sort(Sort.Direction.DESC,"age");
        Pageable pageable = PageRequest.of(page, size,sort);

        List<User> users = template.find(query.with(pageable),User.class);

        return PageableExecutionUtils
                .getPage(
                        users,
                        pageable,
                        ()->template.count(query,User.class)
                );



    }


    public List<User> getUserByName(String name) {
        return this.userDao.findByNameEquals(name);
    }

    public List<User> getUserByDescription(String description) {
        return this.userDao.findByDescriptionIsLike(description);
    }



    public List<User> getUsers() {
        return userDao.findAll();
    }

    public Optional<User> getUser(String id) {
        return this.userDao.findById(id);
    }

    /**
     * 新增和修改都是 save方法，
     * id 存在为修改，id 不存在为新增
     */
    public User createUser(User user) {
        user.setId(null);
        return userDao.save(user);
    }

    public void deleteUser(String id) {
        this.userDao.findById(id)
                .ifPresent(user -> this.userDao.delete(user));
    }

    public void updateUser(String id, User user) {
        this.userDao.findById(id)
                .ifPresent(
                        u -> {
                            u.setName(user.getName());
                            u.setAge(user.getAge());
                            u.setDescription(user.getDescription());
                            this.userDao.save(u);
                        }
                );
    }



    public List<User> findByAgeBetween(Integer from , Integer to){
        return   userDao.findByAgeBetween(from, to);
    }


    public List<User>  findByAgeBetweenAndNameEqualsAndDescriptionIsLike(Integer from , Integer to , String name , String description){
        return userDao.findByAgeBetweenAndNameEqualsAndDescriptionIsLike(from, to, name, description);
    }





}
